//
//  StepsCountSetViewController.m
//  安好
//
//  Created by qinlongqing on 14/11/18.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "StepsCountSetViewController.h"
#import "MyPickerView.h"
#import "UserPhotoAndNameTableViewCell.h"
#import "UserInfoTableViewCell.h"
#import "AppConfig.h"

#define kSheetAccessoryViewHeight    35
#define kSheetViewHeight             220


typedef enum {
    selectSex = 1,
    selectAge,
    selectHeight,
    selectWeight
}SelectType;

@interface StepsCountSetViewController ()<MyPickerViewDelegate,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
{
    MyPickerView *myPickerView;//自定义pickerView
    
    NSMutableArray *sexArray;//性别
    NSMutableArray *ageArray;//年龄
    NSMutableArray *heightArray;//身高
    NSMutableArray *weightArray;//体重

    NSMutableArray *pickerViewDate;//当前的pickerView所需要的数据
    SelectType selectedType;
    NSString *selectedString;//选中的字符串
    NSString *beforeSelectedString;//选中前的字符串

    UIBarButtonItem *commitBtnImg;//提交按钮，只有在有改变时才出现
    
    UITableView *mTableView;
    
    NSDictionary *userInfo;
    
    UserInfoTableViewCell *sexCell;
    UserInfoTableViewCell *ageCell;
    UserInfoTableViewCell *heightCell;
    UserInfoTableViewCell *weightCell;
    
//    MBProgressHUD *HUD;
}
@end

@implementation StepsCountSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makeNavBar];

    //创建自定义pickerView
    myPickerView = [[MyPickerView alloc]init];
    myPickerView.frame = CGRectMake(0, kScreenHeight - 64, kScreenWidth, 200);
    myPickerView.delegate = self;

    
    //性别数据源
    sexArray = [[NSMutableArray alloc]initWithObjects:@"男",@"女", nil];
    //年龄数据源
    ageArray = [[NSMutableArray alloc]init];
    for (NSInteger i=1; i<=130; i++) {
        [ageArray addObject:[NSString stringWithFormat:@"%ld岁",i]];
    }
    //身高数据源
    heightArray = [[NSMutableArray alloc]init];
    for (NSInteger i=100; i<=300; i++) {
        [heightArray addObject:[NSString stringWithFormat:@"%ldCM",i]];
    }
    //体重数据源
    weightArray = [[NSMutableArray alloc]init];
    for (NSInteger i=30; i<=150; i++) {
        [weightArray addObject:[NSString stringWithFormat:@"%ldKg",i]];
    }
    
    mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, kScreenHeight - 64) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    [self.view addSubview:mTableView];
    
    [self getUserInfo];
}

- (void)getUserInfo{
    [self showActionView];
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    [[AFHTTPRequestOperationManager manager] POST:[NSString stringWithFormat:@"%@%@",kBaseURL,kGetUserInfoURL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            
        }
        else{
            userInfo = [responseObject objectForKey:@"data"];
            [mTableView reloadData];
        }
        [self hiddenActionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self hiddenActionView];
    }];
}

//- (void)showInternetActivityIndicator{
//    HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    HUD.center = self.view.center;
//    HUD.dimBackground = NO;
//    HUD.delegate = self;
//}
//
//- (void)hudWasHidden:(MBProgressHUD *)hud{
//#ifdef DEBUGX
//    NSLog(@"Remove HUD from screen when the HUD was hidded");
//#endif
//    [hud removeFromSuperview];
//}


#pragma UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identifier = @"setCell";
        UserPhotoAndNameTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"UserPhotoAndNameTableViewCell" owner:self options:nil] lastObject];
            cell.userPhotoImageView.layer.cornerRadius = 2;
            cell.userPhotoImageView.clipsToBounds = YES;
            [cell.userPhotoImageView setImageWithURL:[NSURL URLWithString:[[UserManager shareInstance] getUserInfo].avatar] placeholderImage:[UIImage imageNamed:@"默认头像"]];
            cell.userNameLabel.text = [[UserManager shareInstance] getUserInfo].username;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else if (indexPath.section == 1){
        static NSString *identifier = @"userInfoCell";
        UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"UserInfoTableViewCell" owner:self options:nil] lastObject];
        }
        if (indexPath.row == 0) {
            cell.photoImageView.image = [UIImage imageNamed:@"jibuStepuser"];
            cell.titleLabel.text = @"性别";
            cell.inputTextField.inputView = myPickerView;
            sexCell = cell;
            cell.inputTextField.delegate = self;
        }
        else if (indexPath.row == 1){
            cell.photoImageView.image = [UIImage imageNamed:@"jibuStepTime"];
            cell.titleLabel.text = @"年龄";
            cell.inputTextField.inputView = myPickerView;
            ageCell= cell;
            cell.inputTextField.delegate = self;
        }
        else if (indexPath.row == 2){
            cell.photoImageView.image = [UIImage imageNamed:@"jibuStepHight"];
            cell.titleLabel.text = @"身高";
            cell.inputTextField.inputView = myPickerView;
            heightCell = cell;
            cell.inputTextField.delegate = self;
        }
        else if (indexPath.row == 3){
            cell.photoImageView.image = [UIImage imageNamed:@"jibuStepWight"];
            cell.titleLabel.text = @"体重";
            cell.inputTextField.inputView = myPickerView;
            weightCell = cell;
            cell.inputTextField.delegate = self;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([textField isEqual:sexCell.inputTextField]) {
        selectedType = selectSex;
        pickerViewDate = sexArray;
        beforeSelectedString = sexCell.inputTextField.text;
        selectedString = [sexArray objectAtIndex:0];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:0 inComponent:0 animated:NO];
    }
    else if ([textField isEqual:ageCell.inputTextField]){
        selectedType = selectAge;
        pickerViewDate = ageArray;
        beforeSelectedString = ageCell.inputTextField.text;
        selectedString = [ageArray objectAtIndex:29];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:29 inComponent:0 animated:NO];
        if (kScreenHeight == IPHONE_HEIGHT) {
            [mTableView setContentOffset:CGPointMake(0, 50)];
        }
    }
    else if ([textField isEqual:heightCell.inputTextField]){
        selectedType = selectHeight;
        pickerViewDate = heightArray;
        beforeSelectedString = heightCell.inputTextField.text;
        selectedString = [heightArray objectAtIndex:70];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:70 inComponent:0 animated:NO];
        if (kScreenHeight == IPHONE_HEIGHT) {
            [mTableView setContentOffset:CGPointMake(0, 100)];
        }
    }
    else if ([textField isEqual:weightCell.inputTextField]){
        selectedType = selectWeight;
        pickerViewDate = weightArray;
        beforeSelectedString = weightCell.inputTextField.text;
        selectedString = [weightArray objectAtIndex:30];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:30 inComponent:0 animated:NO];
    }
    
    [self showOrHiddenSettingImageView];
    
    commitBtnImg.enabled= YES;//有改变，让可以提交
    
    return YES;
}

- (void)myPickerViewCancelSelected:(MyPickerView *)myPickerView{
    if (selectedType == selectSex) {
        sexCell.inputTextField.text = beforeSelectedString;
        [sexCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectAge){
        ageCell.inputTextField.text = beforeSelectedString;
        [ageCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectHeight){
        heightCell.inputTextField.text = beforeSelectedString;
        [heightCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectWeight){
        weightCell.inputTextField.text = beforeSelectedString;
        [weightCell.inputTextField resignFirstResponder];
    }
    if (kScreenHeight == IPHONE_HEIGHT) {
        [mTableView setContentOffset:CGPointMake(0, 0)];
    }

    [self showOrHiddenSettingImageView];
}

- (void)myPickerViewSureSelected:(MyPickerView *)myPickerView{
    if (selectedType == selectSex) {
        sexCell.inputTextField.text = selectedString;
        [sexCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectAge){
        ageCell.inputTextField.text = selectedString;
        [ageCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectHeight){
        heightCell.inputTextField.text = selectedString;
        [heightCell.inputTextField resignFirstResponder];
    }
    else if (selectedType == selectWeight){
        weightCell.inputTextField.text = selectedString;
        [weightCell.inputTextField resignFirstResponder];
    }
    if (kScreenHeight == IPHONE_HEIGHT) {
        [mTableView setContentOffset:CGPointMake(0, 0)];
    }
    
    [self showOrHiddenSettingImageView];
}

- (void)showOrHiddenSettingImageView{
    sexCell.settingImageView.hidden = NO;
    ageCell.settingImageView.hidden = NO;
    heightCell.settingImageView.hidden = NO;
    weightCell.settingImageView.hidden = NO;
    
    if (sexCell.inputTextField.text.length > 0) {
        sexCell.settingImageView.hidden = YES;
    }
    if (ageCell.inputTextField.text.length > 0) {
        ageCell.settingImageView.hidden = YES;
    }
    if (heightCell.inputTextField.text.length > 0) {
        heightCell.settingImageView.hidden = YES;
    }
    if (weightCell.inputTextField.text.length > 0) {
        weightCell.settingImageView.hidden = YES;
    }

    if (selectedType == selectSex) {
        sexCell.settingImageView.hidden = YES;
    }
    else if (selectedType == selectAge){
        ageCell.settingImageView.hidden = YES;
    }
    else if (selectedType == selectHeight){
        heightCell.settingImageView.hidden = YES;
    }
    else if (selectedType == selectWeight){
        weightCell.settingImageView.hidden = YES;
    }
}

#pragma MyPickerViewDelegate
- (NSInteger)numberOfComponentsInMyPickerView:(MyPickerView *)myPickerView{
    return 1;
}

- (NSInteger)myPickerView:(MyPickerView *)myPickerView numberOfRowsInComponent:(NSInteger)component{
    return pickerViewDate.count;
}

- (CGFloat)myPickerView:(MyPickerView *)myPickerView rowHeightForComponent:(NSInteger)component{
    return 25;
}

- (NSString *)myPickerView:(MyPickerView *)myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerViewDate objectAtIndex:row];
}

- (void)myPickerView:(MyPickerView *)myPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    selectedString = [pickerViewDate objectAtIndex:row];
    if (selectedType == selectSex) {
        sexCell.inputTextField.text = selectedString;
    }
    else if (selectedType == selectAge){
        ageCell.inputTextField.text = selectedString;
    }
    else if (selectedType == selectHeight){
        heightCell.inputTextField.text = selectedString;
    }
    else if (selectedType == selectWeight){
        weightCell.inputTextField.text = selectedString;
    }
}


-(void)makeNavBar{
    self.navigationItem.title = @"设置";
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnclicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    // 完成
    commitBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_完成"] style:UIBarButtonItemStylePlain target:self action:@selector(commitBtnClicked)];
    commitBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = commitBtnImg;
    commitBtnImg.enabled = NO;
}

- (void)backBtnclicked{
    [self myPickerViewCancelSelected:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)commitBtnClicked{
//存储
    [self myPickerViewSureSelected:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
